from sklearn import svm
import numpy as np


grangtruth = '/home/d/data/base/Annotations.csv'
feature = open('out.txt').readlines()
gt = open(grangtruth).readlines()[1:]

dg = {}

for x in gt:
    x = x[:-1]
    x = x.split(',')
    dg[x[0]] = int(x[1])

X = []
y = []
for x in feature:
    x = x[:-1]
    x = x.split(',')
    X.append([float(x[n]) for n in range(1, 4)])
    y.append(dg[x[0]])

X = np.array(X)
y = np.array(y)

startNum = 800
trainX = X[:startNum]
trainy = y[:startNum]

testX = X[startNum:]
testy = y[startNum:]


clf = svm.SVC()
clf.fit(X, y)

result = clf.predict(testX)
print((result == testy).sum() / len(testy))

import pickle
import io
f = io.FileIO('model.mod', 'wb')
x = pickle.dumps(clf)
f.write(x)
f.close()


n = io.FileIO('model.mod', 'r')
newclf = pickle.load(n)
newresult = newclf.predict(testX)
print((newresult == testy).sum() / len(testy))


